package cn.wolfcode.crud.web.controller;

import cn.wolfcode.crud.domain.Employee;
import cn.wolfcode.crud.service.IEmployeeService;
import cn.wolfcode.crud.util.JsonResult;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

@Controller
public class UpdatepwdController {
    @Autowired
    private IEmployeeService employeeService;

    @RequestMapping("/updatepwd")
    @ResponseBody
    public JsonResult updatepwd(String oldPassword,String newPassword){
        Subject subject = SecurityUtils.getSubject();
        //拿到当前用户
        Employee employee1 = (Employee)subject.getPrincipal();
        Long id = employee1.getId();

        //去查原密码,若原密码不对报异常,下面程序就不用就走了
        Employee employee = employeeService.selectPassword(id,oldPassword);

            if (employee==null){
                return new JsonResult(false,"密码不对");
            }

        //有原密码执行更新密码
        if (employee != null) {


                employeeService.updatePassword(id,newPassword);
        }
        return  new JsonResult();


    }

    //视图跳转
    @RequestMapping("/updatePwd")
    public String update(){
        return "updatePwd/updatePwd";
    }
}
